package com.jz.yanshe.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.callback.Callback;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.hyphenate.helpdesk.model.ContentFactory;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.GoodsTabResponse;
import com.jz.jd.rest.response.KeFuResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.base.BaseFragment;
import com.jz.yanshe.ui.widget.CheckBoxText;
import com.jz.yanshe.ui.widget.CustomPopWindow;
import com.jz.yanshe.ui.SearchActivity;
import jz.com.feierman.R;

import com.jz.yanshe.util.PreferencesHelper;

/**
 * Created by Administrator on 2018/2/26.
 */

public class MallFragment extends BaseFragment{

    private static MallFragment mallFragment;


    @BindView(R.id.timeline_tablayout)
    TabLayout mTabLayout;
    @BindView(R.id.timeline_viewpager)
    ViewPager mViewPager;
//    @BindView(R.id.banner)
//    Banner banner;
    @BindView(R.id.btn_select)
    TextView btn_select;

    private String user_name;
    private String password;

    private CustomPopWindow mListPopWindow;

    List<String> list = new ArrayList<>();
    ArrayList<CheckBoxText> listType = new ArrayList<>();
    ArrayList<CheckBoxText> listOrder = new ArrayList<>();

    public static MallFragment newInstance() {
        if (mallFragment == null){
            mallFragment = new MallFragment();
            return mallFragment;}
        else{
            return mallFragment;}
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_mall;
    }

    @Override
    protected void initView() {
        mViewPager.setAdapter(new MallPageAdapter(getActivity(), getChildFragmentManager()));
        mTabLayout.setupWithViewPager(mViewPager);
//        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mViewPager.setCurrentItem(0);
        mTabLayout.getTabAt(0).select();
    }

    @Override
    protected void initData() {
//        getBanner();
        getKeFu(new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }


    class MallPageAdapter extends FragmentPagerAdapter {
        private static final int PAGE_COUNT = 4;

        private Context mContext;

        public MallPageAdapter(Context context, FragmentManager fm) {
            super(fm);
            this.mContext = context;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
                case 0:
                    f = HotFragment.newInstance("1");
                    break;
                case 1:
                    f = new RedFragment();
                    break;
                case 2:
                    f = HotFragment.newInstance("3");
                    break;
                case 3:
                    f = HotFragment.newInstance("4");
                    break;
//                case 4:
//                    type = 4;
//                    break;
//                case 5:
//                    type = 5;
//                    break;
//                case 6:
//                    type = 6;
//                    break;
//                case 7:
//                    type = 7;
//                    break;
                default:
                    f = new HotFragment();
                    break;
            }
            return f;
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "热销榜";
                case 1:
                    return "网红搭配";
                case 2:
                    return "现货款";
                case 3:
                    return "预售款";
//                case 4:
//                    return "待退货";
//                case 5:
//                    return "已完成";
//                case 6:
//                    return "已取消";
//                case 7:
//                    return "待退货确认";
                default:
                    return "热销榜";
            }
        }
    }


    @OnClick(R.id.btn_search)
    void btn_search(){
        startActivity(SearchActivity.class);
    }




    @OnClick(R.id.btn_kefu)
    void btn_kefu() {
        if (ChatClient.getInstance().isLoggedInBefore()) {
            Intent intent = new IntentBuilder(getActivity())
                    .setServiceIMNumber("kefuchannelimid_338790") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                    .build();
            startActivity(intent);
        } else {
            getKeFu(new Callback() {
                @Override
                public void onSuccess() {
                    Intent intent = new IntentBuilder(getActivity())
                            .setServiceIMNumber("kefuchannelimid_338790") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                            .setScheduleAgent(ContentFactory.createAgentIdentityInfo("2069427461@qq.com"))
                            .build();
                    startActivity(intent);
                }

                @Override
                public void onError(int i, String s) {

                }

                @Override
                public void onProgress(int i, String s) {

                }
            });
        }
    }
    private void getKeFu(final Callback callback) {
        RestProxy.getInstance().getKeFu(PreferencesHelper.getInstance().getString(getActivity(), "token"), new Observer<KeFuResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(KeFuResponse keFuResponse) {
                ChatClient.getInstance().login(keFuResponse.map.username, keFuResponse.map.password, callback);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void getGoodsTab(){
        RestProxy.getInstance().getGoodsTab(PreferencesHelper.getInstance().getString(getActivity(), "token"), new Observer<GoodsTabResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(GoodsTabResponse goodsTabResponse) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
